//app.js
var util = require('./utils/utils.js');
var api;

App({
  globalData: {
    doubanBase: 'http://t.yushu.im'
  },
  login: function () {
    wx.showLoading({
      title: "正在登录",
      mask: true,
    });
    wx.login({
      success: function (res) {
        console.log(res)
        console.log(res.code)
        // success
        if (res.code) {
          var code = res.code
          wx.getUserInfo({
            success: function (res) {
              console.log(res)
              console.log(res.userInfo.nickName)
              var userInfo = res.userInfo
              wx.setStorageSync('userInfo', {
                nickName: userInfo.nickName,
                avatarUrl: userInfo.avatarUrl
              })
              // success
              // 获取 openid
              var _this = this
              wx.request({
                url: 'https://shipin.lekuaiwang.com/api/Decrypt/index',
                method: 'POST',
                data: {
                  code: code,
                  user_info: res.iv,
                  encrypted_data: res.encryptedData,
                  iv: res.iv,
                  signature: res.signature
                },
                success: function (res) {
                  console.log(900)
                  wx.hideLoading()
                  // success
                  console.log(res)
                  if (res.data.code == 200) {
                    console.log(890)
                    wx.setStorageSync('openid', res.data.data)
                  } else {
                    // wx.showToast({
                    //   title: res.msg
                    // })
                  }
                }
              })

            },
            fail: function () {
              wx.hideLoading()
            }
          })
        } else {
          // console.log(res)
        }
      }
    })
  }
})